package routers

import (
	"server/internal/middleware"
	"server/internal/model"

	_ "server/docs"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func NewRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Logger())
	r.Use(gin.Recovery())
	r.Use(middleware.Cors())
	r.Use(middleware.Translations())

	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	user := NewAccount()
	userInfo := NewUserInfo()
	userApi := r.Group("/user")
	{
		userApi.POST("/register", user.Register)
		userApi.POST("/login", user.Login)
		userApi.POST("/getTransaction", user.GetTransaction)
		userApi.POST("/getInfo", userInfo.GetUserInfo)
		userApi.POST("/setInfo", userInfo.SetUserInfo)
	}

	managerApi := r.Group("/manager")
	managerApi.Use(middleware.JWT(model.ManagerRank))
	{
		managerApi.GET("/users", user.GetAllUsers)
		managerApi.GET("/students", user.GetAllStudents)
		managerApi.GET("/teachers", user.GetAllTeachers)
		managerApi.POST("/remove", user.RemoveUser)
		managerApi.POST("/increaseRank", user.IncreaseRank)
	}

	chainAccount := NewChainAccount()
	chainApi := r.Group("/chain")
	{
		chainApi.POST("/newAccount", chainAccount.NewChainAccount)
		chainApi.POST("/getAccounts", chainAccount.GetChainAccounts)
	}

	course := NewCourse()
	courseApi := r.Group("/course")
	{
		courseApi.POST("/new", course.NewCourse)
		courseApi.POST("/getMy", course.GetMyCourses)
		courseApi.POST("/getCurrent", course.GetCurrentCourses)
		courseApi.POST("/delete", course.DeleteCourse)
	}

	upload := NewUpload()
	r.POST("/upload/file", upload.UploadFile)

	resource := NewResource()
	resourceApi := r.Group("/resource")
	{
		resourceApi.POST("/new", resource.NewResource)
		resourceApi.POST("/getMy", resource.GetMyResources)
		resourceApi.POST("/getOnSale", resource.GetOnSaleResources)
		resourceApi.POST("/delete", resource.DeleteResource)
		resourceApi.POST("/startSale", resource.StartSale)
		resourceApi.POST("/endSale", resource.EndSale)
	}

	return r
}
